package com.example.health.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.health.R;
import com.example.health.bean.Contact;

import java.util.ArrayList;

import static com.example.health.adapter.ContactAdapter.*;


public class ContactAdapter extends RecyclerView.Adapter<ViewHolder> {

    private ArrayList<Contact> mDataList = new ArrayList<>();
    private Context mContext;

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        TextView tvPhone;
        android.support.constraint.ConstraintLayout constraintLayout;

        public ViewHolder(View view) {
            super(view);
            tvName = view.findViewById(R.id.tvName);
            tvPhone = view.findViewById(R.id.tvPhone);
            constraintLayout = view.findViewById(R.id.contactCl);
        }

    }

    public ContactAdapter(Context context) {
        mContext = context;
    }
    public void setData(ArrayList<Contact> list) {
        mDataList = list;
        notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_contact_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @SuppressLint("RecyclerView")
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        final Contact data = mDataList.get(position);
        holder.tvName.setText(data.getName());
        holder.tvPhone.setText(data.getContact_phone());

        holder.constraintLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (callListener != null) {
                    callListener.callClick(data);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }


    private CallListener callListener;

    public void setCallListener(CallListener listener) {
        callListener = listener;
    }

    public interface CallListener {
        void callClick(Contact data);
    }
}